﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics.Contracts;
using System.Collections;

namespace Infrastructure
{
    public static class BaseExtension
    {
        [Pure]
        public static bool IsIn(this string source, List<string> vals)
        {
            try
            {
                if (source != null && !vals.IsEmpty())
                    foreach (string o in vals)
                    {
                        if (string.Compare(o.ToString(), source.ToString(), true) == 0)
                            return true;
                    }
            }
            catch { }
            return false;
        }
        [Pure]
        public static bool IsIn(this string source, params string[] vals)
        {
            try
            {
                return IsIn(source, new List<string>(vals));
            }
            catch { }
            return false;
        }

        [Pure]
        public static bool IsEmpty(this string source)
        {
            return source == null || source.Trim().Length == 0;
        }
        [Pure]
        public static bool IsEmpty(this IEnumerable val)
        {
            return val == null || !val.GetEnumerator().MoveNext();
        }

        [Pure]
        public static bool IsNull(this object value)
        {
            return value == null;
        }
        [Pure]
        public static bool IsNull<T>(this T? value)
            where T : struct
        {
            return !value.HasValue;
        }

        [Pure]
        public static bool IsEqual(this string source, string value)
        {
            return IsEqual(source, value, true);
        }
        [Pure]
        public static bool IsEqual(this string source, string value, bool ignoreCase)
        {
            if (object.Equals(source, value))
                return true;
            else if (source == null || value == null)
                return false;

            return string.Compare(source, value, ignoreCase) == 0;
        }

        [Pure]
        public static IList<T> Append<T>(this IList<T> list, T value)
        {
            list.Add(value);
            return list;
        }

        [Pure]
        public static Int32 ToInt(this string source)
        {
            if (!source.IsEmpty())
            {
                Int32 result;
                if (Int32.TryParse(source, out result)) return result;
            }
            return 0;
        }
        [Pure]
        public static Int32 ToInt(this object s)
        {
            if (s == null || string.IsNullOrEmpty(s.ToString()))
                return 0;
            Int32 dt;
            if (Int32.TryParse(s.ToString(), out dt))
                return dt;
            return 0;
        }

        [Pure]
        public static Double ToDouble(this string source)
        {
            if (source.IsEmpty())
                return 0;
            Double result;
            if (Double.TryParse(source, out result))
                return result;
            return 0;
        }
        [Pure]
        public static Double ToDouble(this object source)
        {
            if (source == null)
                return 0;
            return source.ToString().ToDouble();
        }

        [Pure]
        public static Decimal ToDecimal(this string source)
        {
            if (source.IsEmpty())
                return 0;
            Decimal result;
            if (Decimal.TryParse(source, out result))
                return result;
            return 0;
        }
        [Pure]
        public static Decimal ToDecimal(this object source)
        {
            if (source == null)
                return 0;
            return source.ToString().ToDecimal();
        }
    }
}
